<?php

/**
 * A honlap oldalait reprezentáló osztály.
 * <p>
 * $content: mező tartalma az oldal. Ez egy formázott HTML tartalom.
 * $contoller: ez egy szabályos Yii URL. Ha ez az érték nem üres, akkor az oldal erre az action-re hív.
 * </p>
 *
 * The followings are the available columns in table 'page':
 * @property integer $id
 * @property string $title
 * @property string $description
 * @property string $content
 * @property string $controller
 * @property integer $parent_id
 * @property integer $active
 * @property integer $gallery
 *
 * The followings are the available model relations:
 * @property Page $parent
 * @property Page[] $pages
 */

Yii::import('ext.galleryManager.GalleryBehavior');

class Page extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Page the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);

	}



	/**
	 * (non-PHPdoc)
	 * @see CModel::behaviors()
	 */
	public function behaviors()
	{
	    return array(
	            'galleryBehavior' => array(
	                    'class' => 'GalleryBehavior',
	                    'idAttribute' => 'gallery_id',
	                    'versions' => array(
	                            'small' => array(
	                                    'centeredpreview' => array(100, 100),
	                            ),
	                            'medium' => array(
	                                    'resize' => array(1000, null),
	                            )
	                    ),
	                    'name' => true,
	                    'description' => true,
	            )
	    );
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'page';
	}

	 /**
     * @return array validation rules for model attributes.
     */
    public function rules()
    {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
                array('title, description', 'required', 'message'=>'Ez a mező kötelező.'),
                array('parent_id', 'numerical', 'integerOnly'=>true),
                array('title, controller', 'length', 'max'=>255),

                //array('controller', 'controllerOrContentValidator', 'message'=> 'Ha vezérlőt ad meg, nem lehet tartalma az oldalnak.'),
                // The following rule is used by search().
                // Please remove those attributes that should not be searched.
                array('content, rank', 'unsafe'),
                array('id, title, description, content, controller, parent_id, active, rank', 'safe', 'on'=>'search'),
        );
    }

    /**
     * Egy oldalnak beállítható, hogy melyik controller végezze a renderelését. Ilyenkor viszont nem lehet tartalma.
     *
     * @param string $attribute vizsgált attribútum
     * @param array $params validátor paraméterei
     */
    public function controllerOrContentValidator($attribute, $params){

        if(!empty($this->controller) && !empty($this->content)){
            $this->addError('controller', 'Ha vezérlőd adsz meg, nem adhatsz meg tartalmat.');
        }

    }

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		return array(
			'parent' => array(self::BELONGS_TO, 'Page', 'parent_id'),
			'pages' => array(self::HAS_MANY, 'Page', 'parent_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'title' => 'Cím',
			'description' => 'Leírás',
			'content' => 'Tartalom',
			'controller' => 'Controller',
			'parent_id' => 'Szülő',
			'active' => 'Aktív',
		   'rank'=>'Prioritás',
		);
	}

	/**
	 * (non-PHPdoc)
	 * @see CActiveRecord::defaultScope()
	 */
	public function defaultScope()
	{
	    return array(
	            'order'=>'rank ASC',
	    );
	}

	/**
	 * (non-PHPdoc)
	 * @see CActiveRecord::scopes()
	 */
	public function scopes(){
	    return array(
	            'actives' => array( 'condition' => 'active=1'),
	            'topLevel' => array( 'condition' => 'parent_id is null')
	    );
	}

	//////////////////////// Functional methods ////////////////////////////////////

	/**
	 * (non-PHPdoc)
	 * @see CActiveRecord::beforeSave()
	 */
	public function beforeSave(){
	    $result = parent::beforeSave();

	    if($result){

	        if((int) $this->parent_id == 0){
	            $this->parent_id = null;
	        }

	        return true;

	    }

	    return $result;
	}

	/**
	 *
	 * @return string
	 */
	public function getUrlTitle(){
	   $title = '' . $this->title;
	   $title = str_replace(' ', '-', $title);
	   return strtolower($title);

	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('title',$this->title,true);
		$criteria->compare('description',$this->description,true);
		$criteria->compare('content',$this->content,true);
		$criteria->compare('controller',$this->controller,true);
		$criteria->compare('parent_id',$this->parent_id);
		$criteria->compare('active',$this->active);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
}